package com.jgp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jgp.common.R;
import com.jgp.dto.DishDto;
import com.jgp.entity.Category;
import com.jgp.entity.Dish;
import com.jgp.entity.DishFlavor;
import com.jgp.entity.Employee;
import com.jgp.service.CateGoryService;
import com.jgp.service.DishFlavorService;
import com.jgp.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CateGoryService categoryCService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name ){
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        //    构造分页构造器
        Page<Dish> pageInfo1 = new Page(page,pageSize);
        Page<DishDto> pageInfo2 = new Page();
        //构造条件构造器
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        //    过滤条件
        queryWrapper.like(Strings.isNotEmpty(name),Dish::getName,name);
        //排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        //    执行查询
        dishService.page(pageInfo1,queryWrapper);
// 对象拷贝
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
        List<Dish> records=pageInfo1.getRecords();
        List<DishDto> list=records.stream().map((item)->{
                    DishDto dishDto = new DishDto();
                    BeanUtils.copyProperties(item,dishDto);
                    Long categoryId = item.getCategoryId();
                    Category category = categoryCService.getById(categoryId);
                    if(category!=null){
                    String name1 = category.getName();
                    dishDto.setCategoryName(name1);
                    }
                    return dishDto;
                }
                ).collect(Collectors.toList());

        pageInfo2.setRecords(list);
        return R.success(pageInfo2);

    }
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return  R.success("新增菜品成功");

    }

    /**
     * 根据菜品id查询数据信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto = dishService.getbyidWithFlavor(id);
        return  R.success(dishDto);
    }
    @PutMapping
    public  R<String> update(@RequestBody DishDto dishDto){
        dishService.updatebyidWithFlavor(dishDto);
        return  R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        LambdaQueryWrapper<Dish> lmbdaQueryWrapper = new LambdaQueryWrapper<>();
        lmbdaQueryWrapper.eq(Dish::getStatus,1);
        lmbdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        lmbdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> records=dishService.list(lmbdaQueryWrapper);
        List<DishDto> dishDtoList=records.stream().map((item)->{
                    DishDto dishDto = new DishDto();
                    BeanUtils.copyProperties(item,dishDto);
                    Long categoryId = item.getCategoryId();
                    Category category = categoryCService.getById(categoryId);
                    if(category!=null){
                        String name1 = category.getName();
                        dishDto.setCategoryName(name1);
                    }
            Long dishid = item.getId();
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,dishid);
            List<DishFlavor> list1 = dishFlavorService.list(queryWrapper);
            dishDto.setFlavors(list1);
            return dishDto;
                }
        ).collect(Collectors.toList());

        return R.success(dishDtoList);

    }
}
